from PySide6 import QtWidgets
from PySide6.QtWidgets import QMessageBox
from PySide6.QtGui import QFont
from PySide6.QtCore import Signal, Slot

from util.exercise import Exercise
import util.exercise_file_dao as dao


class ArithmeticPractice(QtWidgets.QWidget):
    exercise: Exercise = None
    index: int = 0

    def set_exercise_and_start(self, exercise: Exercise):
        if len(exercise.equationList) <= 0:
            QMessageBox.warning(self, "提示", "没有生成练习题")
            return

        self.exercise = exercise

        # 重置为1
        self.index = 1

        # 重置回答列表
        self.exercise.clearAnswer()

        # 在标签上的第一个题
        self.lb_arithmetic.setText(exercise.equationList[0].toStringNoAnswer())

    def __init__(self):
        super().__init__()
        self.title = "离线答题"
        self.setWindowTitle(self.title)
        self.setFixedSize(300, 150)

        font = QFont()
        font.setPointSize(20)
        self.lb_arithmetic = QtWidgets.QLabel("1+1=")
        self.lb_arithmetic.setFont(font)
        self.ld_input = QtWidgets.QLineEdit()
        self.btn_home = QtWidgets.QPushButton("返回首页")

        vbox = QtWidgets.QVBoxLayout(self)
        vbox.addWidget(self.lb_arithmetic)
        vbox.addWidget(self.ld_input)
        vbox.addWidget(self.btn_home)

        # 回车填入答案，并显示下一题
        self.ld_input.returnPressed.connect(self.on_enter_pressed)

    @Slot()
    def on_enter_pressed(self):
        if not self.exercise or len(self.exercise.equationList) <= 1:   # 从第二题开始更新
            QMessageBox.warning(self, "提示", "没有生成练习题")
            return

        if self.index > len(self.exercise.equationList):
            # 答题完成后，再次进入
            QMessageBox.warning(self, "提示", "当前习题已经完成练习")
            return

        # todo 如果输入非数字
        # 将输入的结果放入 answer
        num = int(self.ld_input.text())
        self.exercise.answer.append(num)

        # 从第二题开始更新

        if self.index < len(self.exercise.equationList):
            self.lb_arithmetic.setText(
                self.exercise.equationList[self.index].toStringNoAnswer())
        else:
            # 开始检查，应该只调用一次检查
            wrongExercise = self.exercise.check()

            # ①输出错误的习题；②输出错误、正确、总数统计
            self.handle_csv(wrongExercise)

            # 清空习题列表、答案

            # 跳转到首页，同时禁用csv和在线答题的按钮，需要释放一个信号量

        self.index += 1
        self.ld_input.setText("")

    def handle_csv(self, wrongExercise: Exercise):
        text, ok = QtWidgets.QInputDialog.getText(self, "提示", "输入文件名：")
        if not ok:
            print("用户取消输入文件名")
            return
        num, ok = QtWidgets.QInputDialog.getInt(
            self, "提示", "输入每行算式的个数：", value=5)
        if not ok:
            print("用户取消输入算式个数")
            return

        num = num if num else 5

        filename = f"online-{text}-fail.csv"
        dao.write_exercise_to_file(filename, num, wrongExercise)

        fileName = f"online-{text}-statistic.csv"
        dao.write_check_to_file(fileName, wrongExercise)

    # 一维数组变二维数组

    def one_d_to_two_d(self, one_d_array, chunk_size):
        return [one_d_array[i:i + chunk_size] for i in range(0, len(one_d_array), chunk_size)]
